home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / snmp / MibManager.java < prev    next >
Text File  |  1997-06-09  |  7KB  |  268 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. $Id: MibManager.java,v 1.10 1997/05/29 12:54:31 alex Exp $
  6.  
  7. This software maybe be free distributed, any any form, without fee, 
  8. but may not be modified in any way without express permission of 
  9. the directors of Alex Kowalenko Associates Pty Ltd. 
  10.  
  11. Alex Kowalenko Associates Pty Ltd makes no representations or
  12. warranties about the suitabililty of the software, not even the
  13. implied warranty of merchantability or fitness for any particular
  14. purpose.    
  15. */
  16.  
  17. package aka.snmp;
  18.  
  19. import java.util.*;
  20. import java.io.*;
  21.  
  22. /**
  23.  * Responsible for holding the heirachy of managed objects.  Reads them
  24.  * in from a number of different formats
  25.  * @see SnmpObject
  26.  * @version     $Id: MibManager.java,v 1.10 1997/05/29 12:54:31 alex Exp $
  27.  * @author      Alex Kowalenko
  28.  */
  29.  
  30. public class MibManager {
  31.  
  32.   protected Hashtable _tableName; // Map of String(names) -> SnmpObject
  33.   protected Hashtable _tableID;   // Map of ObjectID -> SnmpObject
  34.  
  35. /**
  36.  * Constructor
  37.  */
  38.  
  39.   public MibManager() {
  40.       _tableName = new Hashtable();
  41.       _tableID = new Hashtable();
  42.   };
  43.  
  44.  
  45. /**
  46.  * Insert a new object into the MIB
  47.  */
  48.  
  49.   public boolean insertObject(SnmpObject obj) {
  50.       if(!_tableName.contains(obj.name())) {
  51.       _tableName.put(obj.name(), obj);
  52.       _tableID.put(obj.id(), obj);  
  53.       return true;
  54.       }
  55.       return false;
  56.   };
  57.  
  58. /**
  59.  * Removes all SNMP Objects from this MIB
  60.  */
  61.  
  62.     void deleteAll() {
  63.     _tableName.clear();
  64.     _tableID.clear();
  65.     };
  66.    
  67. /**
  68.  * Dumps all SNMP Objects to standard output.
  69.  */
  70.     
  71.   public void dump() {
  72.       PrintStream e = System.err;
  73.       e.println("-----------------------------------------------------------------");
  74.     
  75.       for(Enumeration enum = _tableName.elements(); enum.hasMoreElements(); ) {
  76.       SnmpObject obj = (SnmpObject) enum.nextElement();
  77.       if(obj.isNode())
  78.           e.println(obj.name() + "\t" + obj.id());
  79.       else
  80.           e.println(obj.name() + "\t" + obj.id()
  81.             + "\t" + obj.type().typeName() 
  82.             + "\t" + obj.access() 
  83.             + "\t" + obj.status());
  84.       }
  85.     
  86.   }
  87.   
  88. /**
  89.  * Dumps all SNMP Objects to standard output. Different means
  90.  * of access
  91.  */
  92.  
  93.   public void dump2() {
  94.       PrintStream e = System.err;
  95.       e.println("-----------------------------------------------------------------");
  96.       e.println("Size : " + _tableID.size());
  97.       for(Enumeration enum = _tableID.elements(); enum.hasMoreElements(); ) {
  98.       SnmpObject obj = (SnmpObject) enum.nextElement();
  99.       if(obj.isNode())
  100.           e.println(obj.name() + "\t" + obj.id());
  101.       else
  102.           e.println(obj.name() + "\t" + obj.id()
  103.             + "\t" + obj.type().typeName() 
  104.             + "\t" + obj.access() 
  105.             + "\t" + obj.status());
  106.       }
  107.       e.println("-keys------------------------------------------------------------");
  108.       e.println("Size : " + _tableID.size());
  109.       for(Enumeration enum = _tableID.keys(); enum.hasMoreElements(); )
  110.       e.println((ObjectId) enum.nextElement());
  111.       e.println("-----------------------------------------------------------------");
  112.   }
  113.  
  114. /**
  115.  * If SNMP Object is present with this name, returns SNMP ID of
  116.  * object.
  117.  */
  118.     
  119.   public ObjectId containsName(String name) {
  120.       SnmpObject obj = (SnmpObject) _tableName.get(name);
  121.       if(obj != null)
  122.       return (ObjectId) obj.id().clone();
  123.       else
  124.       return ObjectId.Void;
  125.   }
  126.  
  127.   
  128. /**
  129.  * Returns SNMP Object with this ID
  130.  */
  131.  
  132.   public SnmpObject contains(ObjectId id) {
  133.       SnmpObject obj = (SnmpObject) _tableID.get(id);
  134.       if(obj != null)
  135.       return obj;
  136.       else
  137.       return SnmpObject.Void;
  138.   };
  139.     
  140. /**
  141.  * Reduces the SNMP ID to the name, with instance variables 
  142.  * attached
  143.  */
  144.   
  145.   public String reduceToName(ObjectId id) {
  146.       SnmpObject obj = contains(id);
  147.       if(obj != SnmpObject.Void)
  148.       return obj.name();
  149.  
  150.       ObjectId testId = (ObjectId) id.clone();
  151.       int instance = testId.removeInstance();
  152.       obj = contains(testId);
  153.       if(obj != SnmpObject.Void) {
  154.       String name = obj.name();
  155.       name += "." +  instance;
  156.       return name;
  157.       }
  158.     
  159.       // Else try from the end of the string and search for a possible name
  160.       Stack instances = new Stack();
  161.       instances.push(new Integer(instance)); // push on first one striped off.
  162.       while(testId.isValid() ) {
  163.       instance = testId.removeInstance();
  164.       instances.push(new Integer(instance));
  165.       obj = contains(testId);
  166.       if(obj != SnmpObject.Void) {
  167.           String name = obj.name();
  168.           while(!instances.isEmpty()) {
  169.           name += "." + ((Integer)instances.pop()).toString();
  170.           }
  171.           return name;
  172.       }
  173.       }
  174.       return null;
  175.   };
  176.     
  177. /**
  178.  * Converts a SNMP ID represented in a dot notation that contains 
  179.  * SNMP Object names and numbers, to one containing only numbers.
  180.  * It does this using SNMP Objects currently stored.
  181.  */
  182.  
  183.   public ObjectId baseForm(String id) {
  184.       StringBuffer decodedID = new StringBuffer();
  185.       StringTokenizer idList = new StringTokenizer(id, ".");
  186.       for(; idList.hasMoreElements();) {
  187.       String item = idList.nextToken();
  188.       if(StringUtil.isNumeric(item)) {
  189.           decodedID.append(item);
  190.           decodedID.append(".");
  191.       }
  192.       else {
  193.           ObjectId replace = containsName(item);
  194.           if(replace.isValid()) {
  195.           decodedID.append(replace.toString());
  196.           decodedID.append('.');
  197.           }
  198.           else {
  199.           return ObjectId.Void;
  200.           }
  201.       }
  202.       }
  203.       decodedID.setLength(decodedID.length() - 1);
  204.       ObjectId returnId = new ObjectId(decodedID.toString());
  205.       return returnId;
  206.   };
  207.  
  208. /**
  209.  * Test harness
  210.  */
  211.     
  212.   public static void main(String args[]) {
  213.       PrintStream out = System.out;
  214.  
  215.       out.println("Creating SNMP MIB MANAGER");
  216.       MibManagerParser mibs = new MibManagerParser();
  217.  
  218.       out.println("Reading SNMP mib");
  219.       try {
  220.       mibs.addFromMibDefFile("mib1.txt");
  221.       } catch(FileNotFoundException e) {
  222.       out.println("File not found");
  223.       return;
  224.       } catch(Exception e) {
  225.       out.println("Exception");
  226.       e.printStackTrace();
  227.       mibs.dump();
  228.       };
  229.  
  230.       //  mibs.dump();
  231.       //  mibs.dump2();
  232.   
  233.       out.println("1.1.3.6.1.2.1.1.3 - " + mibs.containsName("sysUpTime")
  234.           + ", sysUpTime " + mibs.contains(mibs.containsName("sysUpTime")).name()
  235.           + ", TimeTicks "  + mibs.contains(mibs.containsName("sysUpTime")).type().typeName());
  236.           
  237.       out.println("");
  238.       out.println(mibs.containsName("sysUpTime"));
  239.       out.println(new ObjectId(mibs.containsName("sysUpTime").toString()));
  240.       ObjectId id = new ObjectId(mibs.containsName("sysUpTime").toString());
  241.   
  242.       if(id.isValid())
  243.       out.println("sysUpTime " + mibs.contains(id).name() + "\n");
  244.   
  245.       out.println("system.sysName : " + mibs.baseForm("system.1"));
  246.  
  247.       out.println(mibs.containsName("sysName"));
  248.       out.println(mibs.containsName("sysContact"));
  249.     
  250.       ObjectId a = new ObjectId("1.3.6.1.2.1.1.7");
  251.       ObjectId b = new ObjectId("1.3.6.1.2.1.1.6");
  252.  
  253.       out.println(mibs.contains(a).name());
  254.       out.println(mibs.contains(b).name());
  255.  
  256.       out.println("Reduce SNMP ID to names ");
  257.       out.println(a + " reduces to " + mibs.reduceToName(a));
  258.       ObjectId c = new ObjectId("1.3.6.1.2.1.1.10.0");
  259.       ObjectId d = new ObjectId("2");
  260.       ObjectId c1 = new ObjectId("1.3.6.1.2.1.1.3.0");
  261.       out.println(c + " reduces to " + mibs.reduceToName(c));
  262.       out.println(c1 + " reduces to " + mibs.reduceToName(c1));
  263.       out.println(d + " reduces to " + mibs.reduceToName(d));
  264.  
  265.   };
  266.  
  267. };
  268.